/*
 *  ray.h
 *  Ray Tracer
 *
 *  Created by Chirantan Ekbote on 10/8/10.
 *  Copyright 2010 Ohio State University. All rights reserved.
 *
 */

#ifndef _RAY_H_
#define _RAY_H_

#ifdef  __cplusplus
extern "C" {
#endif	// __cplusplus

#include "param.h"
#include "vec3.h"
#include "rgb.h"
	
struct object;	// Forward declaration of object
	
typedef struct ray {
	vec3 origin;
	vec3 direction;
} ray;

static inline vec3 rayPointAt(const ray* r, const float t) {
	return vec3Add(r->origin, vec3Mul(r->direction, t));
}
	
static inline int rayPointIntersect(const ray* r, const vec3 p) {
	vec3 diff = vec3Sub(p, r->origin);
	float t = diff.x / r->direction.x;
	return (t*diff.y < r->direction.y + EPSILON) & 
		   (t*diff.y > r->direction.y - EPSILON) &
	       (t*diff.z < r->direction.z + EPSILON) & 
	       (t*diff.z > r->direction.z - EPSILON);
}

// Returns 1 if we hit an object and stores the index of the object that was hit and the
// time at which it was hit in i and t respectively. Returns 0 if we hit nothing
int castRay(const ray* r, const struct object* shapes, int* i, double* t);

// Returns 1 if there is refraction and stores the direction of the refracted ray in r. 
// Otherwise returns 0
int getRefractionRay(const vec3 normal, const vec3 direction, const float refractIndex, ray* r);
	
#ifdef  __cplusplus
}
#endif	// __cplusplus
#endif	// _RAY_H_